package com.gaohe.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.mapper.ClassesMapper;
import com.gaohe.mapper.UserClassMapper;
import com.gaohe.pojo.Classes;
import com.gaohe.pojo.User;
import com.gaohe.pojo.UserClass;
import com.gaohe.service.UserClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserClassServiceImpl extends ServiceImpl<UserClassMapper, UserClass> implements UserClassService  {
    @Autowired
    UserClassMapper userClassMapper;

    @Autowired
    ClassesMapper classesMapper;
    @Override
    public List<Classes> getClassByUserId(Long id) {
        LambdaQueryWrapper<Classes>lw = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UserClass>lw1 = new LambdaQueryWrapper<>();
         lw1.eq(UserClass::getUserId, id);
        List<UserClass>list = userClassMapper.selectList(lw1);

        for (UserClass userClass : list) {
            lw.eq(Classes::getId,userClass.getClassId()).or();
        }
        List<Classes>list1 = classesMapper.selectList(lw);
        return list1;
    }

    @Override
    public Long getTotal(Long teacherId) {
        QueryWrapper<Classes> lqw = new QueryWrapper<>();
        lqw.lambda().eq(Classes::getTeacher_id,teacherId);
        lqw.select("count(*) as getCount");
        List<Map<String,Object>> maps = classesMapper.selectMaps(lqw);
        return (Long) maps.get(0).get("getCount");
    }

    @Override
    public Long getMouthTotal(Long teacherId) {
        LocalDateTime time1 = LocalDateTime.of(LocalDateTime.now().getYear(),
                LocalDateTime.now().getMonth(),
                1,
                0,0,0
        );
        LocalDateTime time2 = LocalDateTime.of(LocalDateTime.now().getYear(),
                LocalDateTime.now().getMonth(),
                LocalDateTime.now().getMonth().maxLength(),
                23,59,59);
        LambdaQueryWrapper<Classes>lqw = new LambdaQueryWrapper<>();
        lqw.eq(Classes::getTeacher_id,teacherId);
        lqw.between(Classes::getCreateTime,time1,time2);
        Long count = classesMapper.selectCount(lqw);
        return count;
    }

    @Override
    public HashMap<String, Long> getTeacherCount(Long userId) {
        HashMap<String,Long>hashMap = new HashMap<>();
        hashMap.put("allCount",getTotal(userId));
        hashMap.put("mouthCount",getMouthTotal(userId));
        return hashMap;
    }
}
